﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using SwiftMVVM.Commands;
using SwiftMVVM.ChangeTracking;
using SampleApplication.Data;

namespace SampleApplication.Commands
{
    /// <summary>
    /// Contains all of the commands that can be executed on the order manager
    /// </summary>
    public static class OrderManagerCommands
    {
        /// <summary>
        /// Saves all changes to the database
        /// </summary>
        public static RelayCommand<ChangeManager> SaveChanges
        {
            get
            {
                return new RelayCommand<ChangeManager>(changeManager =>
                { 
                    if (changeManager != null) 
                    {
                        try
                        {
                            //SqlAdapter.SubmitChanges(SqlAdapterUpdateMode.Optimistic, changeManager.DeltaList);
                            changeManager.AcceptChanges();
                        }
                        catch (Exception)
                        {
                            //Error occurred log it.
                            throw;
                        }
                    } 
                },
                changeManager => { return changeManager != null && changeManager.HasChanges; });
            }
        }

        /// <summary>
        /// Creates a new random order, adds it to the collection of orders
        /// then moves the current item to the new order
        /// </summary>
        public static RelayCommand<OrderManager> AddOrder
        {
            get
            {
                return new RelayCommand<OrderManager>(orderManager =>
                {
                    var view = CollectionViewSource.GetDefaultView(orderManager.Orders);
                    var newOrder = orderManager.CreateRandomNewOrder();
                    orderManager.Orders.Add(newOrder);
                    view.MoveCurrentTo(newOrder);
                });
            }
        }

        /// <summary>
        /// This command is responsible for decreasing the weight of 
        /// any selected order
        /// </summary>
        public static RelayCommand<Order> DecreaseOrderWeight
        {
            get
            {
                return new RelayCommand<Order>(selectedOrder =>
                {
                    //The first method is what you're going to do when the command is
                    //invoked. The second method returns whether or not this command
                    //is valid for the current state of the object.
                    selectedOrder.Weight -= selectedOrder.Weight == 5 /*min order weight is 5*/ ? 0 : 1;
                }, selectedOrder =>
                {
                    //We only want this increase weight command to be available when
                    //the user actually has a order selected in the grid
                    return selectedOrder != null &&
                        !selectedOrder.Parent.LockOrderWeightAdjustments &&
                        selectedOrder.Weight > 5;
                });
            }
        }

        /// <summary>
        /// This command is responsible for increasing the weight of 
        /// any selected order
        /// </summary>
        public static RelayCommand<Order> IncreaseOrderWeight
        {
            get
            {
                return new RelayCommand<Order>(selectedOrder =>
                {
                    //The first method is what you're going to do when the command is
                    //invoked. The second method returns whether or not this command
                    //is valid for the current state of the object.
                    selectedOrder.Weight += selectedOrder.Weight == 15 /*max order weight is 15*/ ? 0 : 1;
                }, selectedOrder =>
                {
                    //We only want this increase weight command to be available when
                    //the user actually has a order selected in the grid
                    return selectedOrder != null &&
                        !selectedOrder.Parent.LockOrderWeightAdjustments &&
                        selectedOrder.Weight < 15;
                });
            }
        }
    }
}
